<?php
/**
 * Created by PhpStorm.
 * User: chniccs
 * Date: 2019-09-11
 * Time: 9:25
 */

namespace app\admin\controller;


use app\constant\ActivesConst;
use app\helper\ActiveHelper;
use app\model\AdminActives;
use app\model\AdminActiveSku;
use app\model\AdminStores;
use app\util\ReturnCode;
use think\Db;
use think\exception\DbException;

class Actives extends Base
{
    /**
     * 根据id获取数据
     * @return array
     */
    public function get()
    {
        $id = request()->param('id');
        try {
            $model = AdminActives::get($id);
            $sku = (new AdminActiveSku())->where(['aid' => $id, 'del' => 0])->select()->each(function ($value, $key) {
                $value->rule_data;
            })->toArray();
            $data = $model->getData();
            $name = (new AdminStores())->where(['id' => $data['sid']])->field('name')->find();
            $data['store_name'] = $name['name'];
            $data['sku'] = $sku;
            $data['views'] = cache(ActivesConst::VIEWS_TAG . $id);
            return $this->buildSuccess($data);
        } catch (DbException $e) {
        }
        return $this->buildFailed(ReturnCode::INVALID, '获取数据失败');
    }

    public function save()
    {
        $postData = $this->request->post();
        $postData = $this->addTid($postData);
        $result = ActiveHelper::save($postData);
        if($result['done']){
            return $this->buildSuccess(null, '操作成功');
        }else{
            return $this->buildFailed(ReturnCode::FAILED, $result['msg']);
        }
//        $postData = $this->request->post();
//        if($data!=null){
//            $postData = $data;
//        }
//        $postData = $this->addTid($postData);
//        //检测活动时间是否正确设置
//        $checkResult = ActiveHelper::checkDataTime($postData);
//        if(!$checkResult['done']){
//            return $this->buildFailed(ReturnCode::FAILED, '操作失败'.$checkResult['msg']);
//        }
//        //如果不显示就一直是保存状态
//        if($postData['show']==ActivesConst::SHOW_NO){
//            $postData['status'] = ActivesConst::STATUS_SAVED;
//        }else{
//            //否则根据时间来处理当前对应的状态
//            $postData = ActiveHelper::changeStatus($postData);
//        }
//        if(isset($postData['id'])){
//            $result = AdminActives::doSave($postData['id'],$postData);
//        }else{
//            $result = AdminActives::add($postData);
//            $postData = $result;
//        }
//        //发布消息队列
//        if($postData['show']==ActivesConst::SHOW_YES){
//            ActiveHelper::pubQueue($postData);
//            ActiveHelper::pubUseEndQueue($postData);
//        }
//        if ($result) {
//            return $this->buildSuccess(null, '操作成功');
//        } else {
//            return $this->buildFailed(ReturnCode::FAILED, '操作失败');
//        }
    }

    /**
     * 删除
     * @return array
     */
    public function del()
    {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $where = [];
        $where = $this->addTid($where);
        $where['id'] = $id;

        $done = AdminActives::del($where);
        if ($done) {
            return $this->buildSuccess([]);
        }
        return $this->buildFailed(ReturnCode::DELETE_FAILED, '删除失败');
    }

    /**
     * 获取列表
     * @return array
     * @throws \think\exception\DbException
     * @author chniccs
     */
    public function getList()
    {
        $limit = $this->request->get('size', config('apiadmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $type = $this->request->get('type', '', 'intval');
        $select = $this->request->get('select', 0);//是否是选择活动，一般选择活动只能选择未开始及进行中的。且需要显示图片
        $keywords = $this->request->get('keywords', '');
        $obj = new AdminActives();
        if ($type) {
            switch ($type) {
                case 1:
                    $obj = $obj->whereLike('title', "%{$keywords}%");
                    break;
            }
        }

        $where = [];
        //选择活动只能选择未开始及进行中的
        if ($select === 1) {
            $where['status'] = array('in', ActivesConst::STATUS_WAIT . ',' . ActivesConst::STATUS_NORMAL);
        }
        $where = $this->addTid($where);
        $obj = $obj->where($where);
        $obj = $obj->whereNotIn('type',ActivesConst::ACTIVE_TYPE_VIP);
        if ($select == 1) {
            $listObj = $obj->order('create_time DESC')->field('banners,id,title')->paginate($limit, false, ['page' => $start])->toArray();;
        } else {
            $listObj = $obj->order('create_time DESC')->field('id,title,start_time,end_time,type,status')->paginate($limit, false, ['page' => $start])->each(function ($value, $key) {
                $value->sku;
            })->toArray();
        }

        return $this->buildSuccess([
            'list' => $listObj['data'],
            'count' => $listObj['total']
        ]);
    }
}